package com.suneee.logistic.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.suneee.context.page.Page;
import com.suneee.logistic.dao.EnterpriseLogisticDao;
import com.suneee.logistic.entity.EnterpriseLogistics;
import com.suneee.logistic.entity.LogisticsDto;
import com.suneee.orm.GenericDao;
import com.suneee.orm.util.WhereClauseBuilder;

@Repository("enterpriseLogisticDao")
public class EnterpriseLogisticDaoImpl extends GenericDao<EnterpriseLogistics, Integer>
		implements EnterpriseLogisticDao {

	@Override
	public boolean existsCompanyCode(String companyCode, String enterpriseCode) {
		Object[] arg = new Object[2];
		arg[0] = companyCode;
		arg[1] = enterpriseCode;
		List<EnterpriseLogistics> list = this.selectWhere(" company_code = ? and enterprise_code =? ", arg);
		return list != null && list.size() > 0;
	}

	@Override
	public List<EnterpriseLogistics> listLogistic(String enterpriseCode) {
		return this.selectWhere("enterprise_code =?", enterpriseCode);
	}

	@Override
		// TODO Auto-generated method stub
	public void isDefault(boolean isDefault, int configId) {
		StringBuffer sql = new StringBuffer();
		sql.append("update t_enterprise_logistic set ");
		sql.append(" is_default=? ");
		sql.append(" where logistic_id=? ");
		int dt = 0;
		if(isDefault)
		{
			dt = 1;
		}
		Object[] arg = new Object[2];
		arg[0] = dt;
		arg[1] = configId;
		this.executeUpdate(sql.toString(), arg);
	}

	@Override
	public EnterpriseLogistics getDefaultLogistics(String companyCode) {
		EnterpriseLogistics en = null;
		Object[] arg = new Object[2];
		arg[0] = companyCode;
		arg[1] = "1";
		List<EnterpriseLogistics> list = this.selectWhere(" enterprise_code = ? and is_default =? ", arg);
		if(list != null && list.size() >0){
			en = list.get(0);
		}
		return en;
	}

	@Override
	public Page<EnterpriseLogistics> pageEnLogistics(LogisticsDto dto) {
		WhereClauseBuilder wcb = new WhereClauseBuilder();
		wcb.equals("company_code",dto.getCompanyCode());
		wcb.contains("company_name", dto.getCompanyName());
		wcb.equals("enterprise_code",dto.getEnterpriseCode());
		
		return this.selectWhere(wcb.getSql(), dto, wcb.getArgs());
	}

	@Override
	public EnterpriseLogistics getEnterpriseLogistics(String companyName, String enterpriseCode) {
		WhereClauseBuilder wcb = new WhereClauseBuilder();
		wcb.equals("company_name", companyName);
		wcb.equals("enterprise_code",enterpriseCode);
		List<EnterpriseLogistics> list = this.selectWhere(wcb.getSql(), wcb.getArgs());
		if(list != null && list.size() > 0){
			return list.get(0);
		}
		return null;
	}

}
